package com.wongoing.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.wongoing.jwt.JwtUserModel;
import com.wongoing.util.ResourceUtil;
/**
 * 类功能说明 TODO: 项目参数工具类
 * 类修改者	修改日期
 * 修改说明
 * @author yuany yuany@mesnac.com
 * @date 2016-5-20 17:21:14
 * @version V1.0
 */
public class ResourceUtil {

	private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("config");

	/**
	 * 获得sessionInfo名字
	 * 
	 * @return
	 */
	public static final String getSessionInfoName() {
		return bundle.getString("sessionInfoName");
	}
	
	public static final String getDevMode() {
		return bundle.getString("devMode");
	}

	/**
	 * 获得上传表单域的名称
	 * 
	 * @return
	 */
	public static final String getUploadFieldName() {
		return bundle.getString("uploadFieldName");
	}

	/**
	 * 获得上传文件的最大大小限制
	 * 
	 * @return
	 */
	public static final long getUploadFileMaxSize() {
		return Long.valueOf(bundle.getString("uploadFileMaxSize"));
	}

	/**
	 * 获得允许上传文件的扩展名
	 * 
	 * @return
	 */
	public static final String getUploadFileExts() {
		return bundle.getString("uploadFileExts");
	}

	/**
	 * 获得上传文件要放到那个目录
	 * 
	 * @return
	 */
	public static final String getUploadDirectory() {
		return bundle.getString("uploadDirectory");
	}
	
	/**
	 * 获得上传头像的最大大小限制2MB
	 * 
	 * @return
	 */
	public static final long getUploadImageMaxSize() {
		return Long.valueOf(bundle.getString("uploadImageMaxSize"));
	}

	/**
	 * 获得允许上传头像的文件扩展名
	 * 
	 * @return
	 */
	public static final String getUploadImageExts() {
		
		return bundle.getString("uploadImageExts");
	}

	/**
	 * 获得上传头像要放到那个绝对路径
	 * 
	 * @return
	 */
	public static final String getUploadImageAbsoluteRoot() {
		String path = ResourceUtil.class.getResource("/").getPath().replaceAll("WEB-INF/classes/", "").substring(1);
		path = path + bundle.getString("uplaodImageRelativeRoot");
		return path;
		//return bundle.getString("uploadImageAbsoluteRoot");
	}
	/**
	 * 功能说明：获取项目路径
	 * 修改说明：
	 * @author 吴兆强
	 * @date 2017年12月5日 上午8:38:50
	 * @return
	 */
	public static final String getUploadImageProjectRoot() {
		String path = ResourceUtil.class.getResource("/").getPath().replaceAll("WEB-INF/classes/", "").substring(1);
		return path;
		//return bundle.getString("uploadImageAbsoluteRoot");
	}
	/**
	 * 功能说明：获取存储图片的相对路径
	 * 修改说明：
	 * @author 吴兆强
	 * @date 2017年10月26日 下午8:55:32
	 * @return
	 */
	public static final String getUploadImageRelativeRoot() {
	String path = ResourceUtil.class.getResource("/").getPath().replaceAll("WEB-INF/classes/", "").substring(1);
//		path = path + bundle.getString("uploadImageRelativeRoot");
//		return path;
		//return bundle.getString("uploadImageAbsoluteRoot");
		return com.wongoing.bus.util.Constants.IMG_UPLOAD_BASE+"\\";
	}
	
	/**
	 * 获得上传头像要放到那个虚拟目录
	 * 
	 * @return
	 */
	public static final String getUplaodImageRelativeRoot() {
		return bundle.getString("uplaodImageRelativeRoot");
	}
	
	/**
	 * 获得上传头像要放到那个虚拟目录
	 * 
	 * @return
	 */
	public static final String getUploadImgRelativeRoot() {
		return bundle.getString("uploadImageRelativeRoot");
	}
	
	/**
	 * 获得请求路径
	 * 
	 * @param request
	 * @return
	 */
	public static String getRequestPath(HttpServletRequest request) {
		String requestPath = request.getRequestURI();
//		if(request.getQueryString()!=null && !"".equals(request.getQueryString())){
//			requestPath += "?" + request.getQueryString();
//		}
//		if (requestPath.indexOf("&") > -1) {// 去掉其他参数
//			requestPath = requestPath.substring(0, requestPath.indexOf("&"));
//		}
		requestPath = requestPath.substring(request.getContextPath().length() + 1);// 去掉项目路径
		return requestPath;
	}
	
	/**
	 * 获得请求路径
	 * 
	 * @param request
	 * @return
	 */
	public static String getRequestPathAndGetParam(HttpServletRequest request) {
		String requestPath = request.getRequestURI();
		if(request.getQueryString()!=null && !"".equals(request.getQueryString())){
			requestPath += "?" + request.getQueryString();
		}
		requestPath = requestPath.substring(request.getContextPath().length() + 1);// 去掉项目路径
		return requestPath;
	}
	
	/**
	 * 获得请求路径
	 * 
	 * @param request
	 * @return
	 */
	public static String getPostParam(HttpServletRequest request) {
		String params = " [ ";
		Map<String, Object> paramMap = getParameterMap(request);
		for(String key : paramMap.keySet()){
			if(StringUtils.isNotBlank(request.getQueryString()) && !request.getQueryString().contains(key)){
				params = params + key + ":" + paramMap.get(key) + ",";
			}
		}
		params = params + " ]";
		return params;
	}
	
	public static Map<String, Object> getParameterMap(HttpServletRequest request) {
	    // 参数Map
	    Map properties = request.getParameterMap();
	    // 返回值Map
	    Map<String, Object> returnMap = new HashMap<String, Object>();
	    Iterator entries = properties.entrySet().iterator();
	    Map.Entry entry;
	    String name = "";
	    String value = "";
	    while (entries.hasNext()) {
	        entry = (Map.Entry) entries.next();
	        name = (String) entry.getKey();
	        Object valueObj = entry.getValue();
	        if(null == valueObj){
	            value = "";
	        }else if(valueObj instanceof String[]){
	            String[] values = (String[])valueObj;
	            for(int i=0;i<values.length;i++){
	                value = values[i] + ",";
	            }
	            value = value.substring(0, value.length()-1);
	        }else{
	            value = valueObj.toString();
	        }
	        returnMap.put(name, value);
	    }
	    return returnMap;
	}
	
	/**
	 * SpringMvc下获取request
	 * 
	 * @return
	 */
	public static HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request;

	}
	/**
	 * SpringMvc下获取session
	 * 
	 * @return
	 */
	public static HttpSession getSession() {
		HttpSession session = getRequest().getSession();
		return session;

	}
	
	public static String getTokenid() {
		String tokenid = getRequest().getParameter("tokenid");
		return tokenid;

	}
	
	public static  final JwtUserModel getCurrentUser(){
		return (JwtUserModel) getSession().getAttribute(com.wongoing.bus.util.Constants.CURRENT_USER_ID);		
	}
	
	
	/**
	 * 获得文章的上传文件要放到那个绝对路径
	 * 
	 * @return
	 */
	public static final String getUploadFileAbsoluteRoot() {
		String path = ResourceUtil.class.getResource("/").getPath().replaceAll("WEB-INF/classes/", "").substring(1);
		path = path + bundle.getString("uploadFileRelativeRoot");
		return path;
		//return bundle.getString("uploadImageAbsoluteRoot");
	}
	/**
	 * 获得文章的上传文件要放到那个相对路径
	 * 
	 * @return
	 */
	public static final String getUploadFileRelativeRoot() {
		String path= bundle.getString("uploadFileRelativeRoot");
		return path;
		//return bundle.getString("uploadImageAbsoluteRoot");
	}
}
